package eclipse.util;

/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class CodeTimer 
{
	//System time when the code timer starts
	private long start;
	//System time when the code timer ends
	private long end;
	//Difference between the end time and the start time.
	private long processTime;
	//description for what is being timed.
	private String code = "functions";
	
	/**
	 *  Initializes the Code timer using the default description
	 *  This uses the default description.
	 */
	public CodeTimer(){}
	/**
	 *  Initializes the code timer using a custom description
	 * @param code Description to use for the Code timer
	 */
	public CodeTimer(String code)
	{
		this.code = code;
	}
	/**
	 * Starts the code timer and sets the start value to the current System time.
	 */
	public void start()
	{
		start = System.nanoTime();
	}
	/**
	 * Stops the code timer and sets the end value to the current System time.
	 */
	public void stop()
	{
		//save the end time
		end = System.nanoTime();
		//calculate the time it took to execute the functions.
		processTime = end - start;
	}
	/**
	 * Returns the time it took to process the functions between when "start" and "end" were called.
	 * @return The time that it took to execute the specific functions as a 64bit integer.
	 */
	public long getResults()
	{		
		return processTime;
	}
	/**
	 * Prints the results to the console. Used for debugging purposes.
	 */
	public void printResults()
	{
		//calculate the seconds it took to execute the functions by conversion
		final String time = "" + ((double)processTime * 0.000000001d);
		//output to the Console
		System.out.println("Execution took for " + code + ": " + time + " seconds");
	}
}
